package com.tencent.sr.iris.basic.saas.dependency.group;

import cn.hutool.core.collection.CollectionUtil;
import com.tencent.sr.iris.activity.interfaces.client.task.GroupTaskCoreClient;
import com.tencent.sr.iris.activity.interfaces.client.task.IrisGroupTaskBoardClient;
import com.tencent.sr.iris.activity.interfaces.request.group.AdvisorGroupTaskRequest;
import com.tencent.sr.iris.activity.interfaces.request.group.AdvisorGroupTaskSecondRequest;
import com.tencent.sr.iris.activity.interfaces.request.group.AdvisorGroupTaskThirdRequest;
import com.tencent.sr.iris.activity.interfaces.request.group.BaseRequest;
import com.tencent.sr.iris.activity.interfaces.request.group.GroupTaskRequest;
import com.tencent.sr.iris.activity.interfaces.request.group.GroupTaskSecondRequest;
import com.tencent.sr.iris.activity.interfaces.request.group.RankConfigRequest;
import com.tencent.sr.iris.activity.interfaces.request.group.TaskStatisticsRequest;
import com.tencent.sr.iris.activity.interfaces.request.task.TaskCommissionRequest;
import com.tencent.sr.iris.activity.interfaces.response.group.*;
import com.tencent.sr.iris.activity.interfaces.response.group.operate.OperateSecondBoardResponse;
import com.tencent.sr.iris.activity.interfaces.response.group.task.TaskCommissionResponse;
import com.tencent.sr.iris.basic.saas.common.enums.GroupTaskRoleEnum;
import com.tencent.sr.iris.basic.saas.common.enums.TaskTargetEvaluationEnum;
import com.tencent.sr.iris.user.core.service.interfaces.client.user.IrisUserInfoClient;
import com.tencent.sr.iris.user.core.service.interfaces.client.user.UserUtilClient;
import com.tencent.sr.iris.user.core.service.interfaces.dto.IrisUserInfoDTO;
import com.tencent.sr.iris.user.core.service.interfaces.enums.StaffFromTypeEnum;
import com.tencent.sr.iris.user.core.service.interfaces.enums.UserLevelEnum;
import com.tencent.sr.iris.user.core.service.interfaces.request.user.UserChannelSwitchRequest;
import com.tencent.sr.iris.user.core.service.interfaces.request.user.UserInfoQueryRequest;
import com.tencent.sr.iris.user.core.service.interfaces.response.user.UserChannelSwitchResponse;
import com.tencent.sr.iris.user.core.service.interfaces.response.user.UserInfoQueryResponse;
import com.tencent.sr.rmall.common.primitive.HttpResult;
import com.tencent.sr.rmall.common.request.PaginationRequest;
import com.tencent.sr.rmall.common.response.PaginationResponse;
import java.util.List;
import java.util.Objects;
import javax.annotation.Resource;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.collections4.CollectionUtils;
import org.apache.commons.lang.StringUtils;
import org.apache.logging.log4j.util.Strings;
import org.springframework.stereotype.Component;

@Component
@Slf4j
public class GroupTaskBoardDependency {
    @Resource
    private IrisGroupTaskBoardClient irisGroupTaskBoardClient;
    @Resource
    private IrisUserInfoClient irisUserInfoClient;
    @Resource
    private UserUtilClient userUtilClient;
    @Resource
    private GroupTaskCoreClient groupTaskCoreClient;

     public HttpResult<PaginationResponse<GroupTaskBoardResponse>> queryGroupTaskBoard(
             PaginationRequest<GroupTaskRequest> request) {
        if (Objects.isNull(request.getQueryCriteria())) {
            request.setQueryCriteria(new GroupTaskRequest());
        }
        return irisGroupTaskBoardClient.queryGroupTaskBoard(request);
    }

    public HttpResult<PaginationResponse<GroupTaskBoardSecondResponse>> queryGroupTaskSecondBoardSpecialSprint(
            PaginationRequest<GroupTaskSecondRequest> request) {
        if (Objects.isNull(request.getQueryCriteria())) {
            request.setQueryCriteria(new GroupTaskSecondRequest());
        }
        return irisGroupTaskBoardClient.queryGroupSecondTaskBoard(request);
    }
    
     public HttpResult<PaginationResponse<GroupTaskBoardSecondResponse>> queryGroupTaskSecondBoardInvite(
             PaginationRequest<GroupTaskSecondRequest> request) {
        if (Objects.isNull(request.getQueryCriteria())) {
            request.setQueryCriteria(new GroupTaskSecondRequest());
        }
        return irisGroupTaskBoardClient.queryGroupSecondTaskBoard(request);
    }
    
     public HttpResult<PaginationResponse<GroupTaskBoardSecondResponse>> queryGroupTaskSecondBoardFirstOrder(
             PaginationRequest<GroupTaskSecondRequest> request) {
        if (Objects.isNull(request.getQueryCriteria())) {
            request.setQueryCriteria(new GroupTaskSecondRequest());
        }
        return irisGroupTaskBoardClient.queryGroupSecondTaskBoard(request);
    }
    
     public HttpResult<PaginationResponse<GroupTaskBoardSecondResponse>> queryGroupTaskSecondBoardSprint(
             PaginationRequest<GroupTaskSecondRequest> request) {
        if (Objects.isNull(request.getQueryCriteria())) {
            request.setQueryCriteria(new GroupTaskSecondRequest());
        }
        return irisGroupTaskBoardClient.queryGroupSecondTaskBoard(request);
    }
    
     public HttpResult<PaginationResponse<GroupNormalTaskSecondResponse>> queryGroupTaskSecondBoardNormal(
             PaginationRequest<GroupTaskSecondRequest> request) {
        if (Objects.isNull(request.getQueryCriteria())) {
            request.setQueryCriteria(new GroupTaskSecondRequest());
        }
        return irisGroupTaskBoardClient.queryGroupTaskSecondBoardNormal(request);
    }
    
    public HttpResult<PaginationResponse<AdvisorBoardResponse>> queryAdvisorBoard(
             PaginationRequest<AdvisorGroupTaskRequest> request) {
        AdvisorGroupTaskRequest queryCriteria = request.getQueryCriteria();

        if (Objects.isNull(queryCriteria)) {
            queryCriteria = new AdvisorGroupTaskRequest();
            request.setQueryCriteria(queryCriteria);
        }

        List<Integer> taskTargetEvaluation = queryCriteria.getTaskTargetEvaluation();
        if (CollectionUtils.isNotEmpty(taskTargetEvaluation) &&
                taskTargetEvaluation.contains(TaskTargetEvaluationEnum.TRAINEE_LEVEL_UP.getCode())) {
            taskTargetEvaluation.add(TaskTargetEvaluationEnum.MEMBER_LEVEL_UP.getCode());
            taskTargetEvaluation.add(TaskTargetEvaluationEnum.SENIOR_LEVEL_UP.getCode());
        }

        return irisGroupTaskBoardClient.queryAdvisorBoard(request);
    }
    
    public HttpResult<PaginationResponse<AdvisorBoardInviteSecondResponse>> queryAdvisorSecondBoardInvite(
             PaginationRequest<AdvisorGroupTaskSecondRequest> request) {
        if (Objects.isNull(request.getQueryCriteria())) {
            request.setQueryCriteria(new AdvisorGroupTaskSecondRequest());
        }
        return irisGroupTaskBoardClient.queryAdvisorSecondBoardInvite(request);
    }

    public HttpResult<PaginationResponse<AdvisorBoardFirstOrderSecondResponse>> queryAdvisorSecondBoardFirstOrder(
             PaginationRequest<AdvisorGroupTaskSecondRequest> request) {
        if (Objects.isNull(request.getQueryCriteria())) {
            request.setQueryCriteria(new AdvisorGroupTaskSecondRequest());
        }
        return irisGroupTaskBoardClient.queryAdvisorSecondBoardFirstOrder(request);
    }
    
    public HttpResult<GroupTaskPaginationResponse<AdvisorBoardSprintSecondResponse>> queryAdvisorSecondBoardSprint(
             PaginationRequest<AdvisorGroupTaskSecondRequest> request) {
        if (Objects.isNull(request.getQueryCriteria())) {
            request.setQueryCriteria(new AdvisorGroupTaskSecondRequest());
        }
        return irisGroupTaskBoardClient.queryAdvisorSecondBoardSprint(request);
    }
    
    public HttpResult<PaginationResponse<AdvisorBoardCommissionSecondResponse>> queryAdvisorSecondBoardCommission(
             PaginationRequest<AdvisorGroupTaskSecondRequest> request) {
        if (Objects.isNull(request.getQueryCriteria())) {
            request.setQueryCriteria(new AdvisorGroupTaskSecondRequest());
        }
        return irisGroupTaskBoardClient.queryAdvisorSecondBoardCommission(request);
    }
    
    public HttpResult<PaginationResponse<AdvisorNormalTaskSecondResponse>> queryAdvisorSecondBoardNormal(
             PaginationRequest<AdvisorGroupTaskSecondRequest> request) {
        if (Objects.isNull(request.getQueryCriteria())) {
            request.setQueryCriteria(new AdvisorGroupTaskSecondRequest());
        }
        return irisGroupTaskBoardClient.queryAdvisorSecondBoardNormal(request);
    }

    public HttpResult<GroupTaskPaginationResponse<OperateSecondBoardResponse>> queryAdvisorSecondSpecialSprint(
            PaginationRequest<AdvisorGroupTaskSecondRequest> request) {
        if (Objects.isNull(request.getQueryCriteria())) {
            request.setQueryCriteria(new AdvisorGroupTaskSecondRequest());
        }
        return irisGroupTaskBoardClient.queryAdvisorSecondBoard(request);
    }
    
    public HttpResult<PaginationResponse<GroupTaskBoardSecondResponse>> queryAdvisorThirdBoardInvite(
             PaginationRequest<AdvisorGroupTaskThirdRequest> request) {
        if (Objects.isNull(request.getQueryCriteria())) {
            request.setQueryCriteria(new AdvisorGroupTaskThirdRequest());
        }
        return irisGroupTaskBoardClient.queryAdvisorThirdBoardInvite(request);
    }
    
    public HttpResult<PaginationResponse<GroupTaskBoardSecondResponse>> queryAdvisorThirdBoardFirstOrder(
             PaginationRequest<AdvisorGroupTaskThirdRequest> request) {
        if (Objects.isNull(request.getQueryCriteria())) {
            request.setQueryCriteria(new AdvisorGroupTaskThirdRequest());
        }
        return irisGroupTaskBoardClient.queryAdvisorThirdBoardFirstOrder(request);
    }
    
    public HttpResult<PaginationResponse<GroupTaskBoardSecondResponse>> queryAdvisorThirdBoardSprint(
             PaginationRequest<AdvisorGroupTaskThirdRequest> request) {
        if (Objects.isNull(request.getQueryCriteria())) {
            request.setQueryCriteria(new AdvisorGroupTaskThirdRequest());
        }
        return irisGroupTaskBoardClient.queryAdvisorThirdBoardSprint(request);
    }
    
    public HttpResult<PaginationResponse<AdvisorBoardCommissionThirdResponse>> queryAdvisorThirdBoardCommission(
             PaginationRequest<AdvisorGroupTaskThirdRequest> request) {
        if (Objects.isNull(request.getQueryCriteria())) {
            request.setQueryCriteria(new AdvisorGroupTaskThirdRequest());
        }
        return irisGroupTaskBoardClient.queryAdvisorThirdBoardCommission(request);
    }

    
    public HttpResult<PaginationResponse<GroupNormalTaskSecondResponse>> queryAdvisorThirdBoardNormal(
             PaginationRequest<AdvisorGroupTaskThirdRequest> request) {
        if (Objects.isNull(request.getQueryCriteria())) {
            request.setQueryCriteria(new AdvisorGroupTaskThirdRequest());
        }
        return irisGroupTaskBoardClient.queryAdvisorThirdBoardNormal(request);
    }
    
    public HttpResult<TaskStatisticsResponse> queryAdvisorTaskStatistics( TaskStatisticsRequest req) {
        if (Objects.isNull(req)) {
            req = new TaskStatisticsRequest();
        }
        return irisGroupTaskBoardClient.queryAdvisorTaskStatistics(req);
    }
    
    public HttpResult<TaskStatisticsResponse> queryOperateTaskStatistics( TaskStatisticsRequest req) {
        if (Objects.isNull(req)) {
            req = new TaskStatisticsRequest();
        }
        return irisGroupTaskBoardClient.querySellerTaskStatistics(req);
    }

    public HttpResult<PaginationResponse<GroupTaskBoardSecondResponse>> queryGroupTaskSecondBoardRank(
            PaginationRequest<GroupTaskSecondRequest> request) {
        if (Objects.isNull(request.getQueryCriteria())) {
            request.setQueryCriteria(new GroupTaskSecondRequest());
        }
        return irisGroupTaskBoardClient.queryGroupSecondTaskBoard(request);
    }

    public HttpResult<PaginationResponse<AdvisorBoardRankSecondResponse>> queryAdvisorSecondBoardRank(
            PaginationRequest<AdvisorGroupTaskSecondRequest> request) {
        if (Objects.isNull(request.getQueryCriteria())) {
            request.setQueryCriteria(new AdvisorGroupTaskSecondRequest());
        }
        return  irisGroupTaskBoardClient.queryAdvisorSecondBoardRank(request);
    }

    public HttpResult<PaginationResponse<GroupTaskBoardSecondResponse>> queryAdvisorThirdBoardRank(
            PaginationRequest<AdvisorGroupTaskThirdRequest> request) {
        if (Objects.isNull(request.getQueryCriteria())) {
            request.setQueryCriteria(new AdvisorGroupTaskThirdRequest());
        }
        return irisGroupTaskBoardClient.queryAdvisorThirdBoardRank(request);
    }

    public HttpResult<PaginationResponse<GroupTaskBoardSecondResponse>> queryAdvisorThirdSpecialSprint(
            PaginationRequest<AdvisorGroupTaskThirdRequest> request) {
        if (Objects.isNull(request.getQueryCriteria())) {
            request.setQueryCriteria(new AdvisorGroupTaskThirdRequest());
        }
        return irisGroupTaskBoardClient.queryAdvisorThirdBoard(request);
    }

    public HttpResult<List<RankAwardConfigResponse>> queryRankConfigs(RankConfigRequest request) {
        if (Objects.isNull(request)) {
            return  new HttpResult<>();
        }
        return irisGroupTaskBoardClient.queryRankConfigs(request);
    }

    public HttpResult<TaskCommissionResponse> taskReward(BaseRequest request) {
        HttpResult<TaskCommissionResponse> result = new HttpResult<>();
        if (Objects.isNull(request)) {
            return result;
        }
        String unionId = request.getUnionId();
        String uid = request.getUid();

        if (!StringUtils.isNotBlank(unionId)) {
            unionId = getUnionIdByUid(request.getSaasId(), uid);
        }
        if (StringUtils.isNotBlank(unionId)) {
            TaskCommissionRequest taskCommissionRequest = new TaskCommissionRequest();
            taskCommissionRequest.setUnionId(unionId);
            taskCommissionRequest.setUid(uid);
            result = groupTaskCoreClient.taskReward(taskCommissionRequest);
        }
        return result;
    }

    public HttpResult<Integer> queryUserIdentity(String uid) {
        UserInfoQueryRequest request = new UserInfoQueryRequest();
        request.setUid(uid);
        HttpResult<UserInfoQueryResponse> response = irisUserInfoClient.queryUserInfo(request);
        UserInfoQueryResponse data = response.getData();
        if (Objects.nonNull(data) && Objects.nonNull(data.getUserInfoDTO())) {
            IrisUserInfoDTO irisUserInfo = data.getUserInfoDTO();
            String levelId = irisUserInfo.getLevelId();
            Byte type = irisUserInfo.getType();
            if (Objects.nonNull(levelId) && Objects.nonNull(type)) {
                //见习会员及以上等级的外部用户 为团长
                if (StaffFromTypeEnum.isOuterSign(type.toString()) && UserLevelEnum.GROUP_LIST.contains(levelId)) {
                    return HttpResult.succ(GroupTaskRoleEnum.GROUP_ROLE.getStatus());
                } else if (StaffFromTypeEnum.TUTOR.getCode().equals(type.toString())) {
                    return HttpResult.succ(GroupTaskRoleEnum.ADVISOR_ROLE.getStatus());
                }
            }
        }
        return HttpResult.succ(GroupTaskRoleEnum.OTHER_ROLE.getStatus());
    }

    public String getUnionIdByUid(Long saasId,String account) {
        try {
            UserChannelSwitchRequest request = new UserChannelSwitchRequest();
            request.setSaasId(saasId);
            request.setAccount(account);
            HttpResult<UserChannelSwitchResponse> response = userUtilClient.getUnionIdByUid(request);
            log.info("调用GroupTaskBoardDependency.getUnionIdByUid，req：{}，response：{}", request, response);
            if (response != null && response.getData() != null && CollectionUtil.isNotEmpty(
                    response.getData().getChannelInfoList())) {
                return response.getData().getChannelInfoList().get(0).getChannelAccount();
            }
        } catch (Exception e) {
            // web端查询用户unionId，若出现异常，暂不影响其他数据展示
            log.error("GroupTaskBoardDependency-getUnionIdByUid error", e);
        }
        return Strings.EMPTY;
    }

}
